//
//  LoginViewController.m
//  JSXDiscuz
//
//  Created by 周思扬 on 2018/4/12.
//  Copyright © 2018年 周思扬. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "InputTextFieldView.h"

#define SumCount 60

@interface LoginViewController ()
{
    BOOL hasSend;
}
@property(nonatomic,weak) InputTextFieldView * inputView;
@property(nonatomic,weak) UILabel * titleLab;
@property(nonatomic,weak) UIImageView * logoImgV;
@property(nonatomic,weak) UIButton * weixinBtn;
@property(nonatomic,weak) UIButton * phoneBtn;

@property(nonatomic,strong) UserData *user;

@end

@implementation LoginViewController

/*
#pragma mark - 保证LoginViewController单例
// 创建静态对象 防止外部访问
static LoginViewController *_instance;
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    });
    return _instance;
}
// 为了使实例易于外界访问 我们一般提供一个类方法
// 类方法命名规范 share类名|default类名|类名



// 为了严谨，也要重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
    return _instance;
}
*/

+(instancetype)shareLoginViewController
{
    //return _instance;
    // 最好用self 用Tools他的子类调用时会出现错误
    return [[self alloc]init];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubView];
}

-(void)setSubView
{
    UILabel * label=[[UILabel alloc]init];
    label.text=@"手机号登录";
    label.textColor=SDColor(69, 69, 69);
    label.textAlignment=NSTextAlignmentCenter;
    label.font=SDFontOf15;
    [self.view addSubview:label];
    label.hidden=YES;
    self.titleLab=label;
    
    UIImageView * imgv=[[UIImageView alloc]init];
    self.logoImgV=imgv;
    imgv.backgroundColor=[UIColor redColor];
    imgv.contentMode=UIViewContentModeCenter;
    [imgv setImage:[UIImage imageNamed:@"person_logo"]];
    [self.view addSubview:imgv];
    
    
    InputTextFieldView * fieldView=[InputTextFieldView shareView];
    [self.view addSubview:fieldView];
    fieldView.alpha=0.0;
    self.inputView=fieldView;
    
    UIButton * btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn1];
    btn1.imageEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 30);
    btn1.layer.cornerRadius=5;
    btn1.backgroundColor=SDColor(58, 179, 69);
    [btn1 setTitle:@"微信用户快速登录" forState:UIControlStateNormal];
    btn1.titleLabel.font=SDFontOf14;
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"person_weixin"] forState:UIControlStateNormal];
    self.weixinBtn=btn1;
    
    UIButton * btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn2];
    btn2.imageEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 30);
    btn2.layer.cornerRadius=5;
    btn2.layer.borderWidth=1;
    btn2.layer.borderColor=SDColor(69, 69, 69).CGColor;
    btn2.backgroundColor=[UIColor whiteColor];
    [btn2 setTitle:@"手机号验证登录" forState:UIControlStateNormal];
    btn2.titleLabel.font=SDFontOf14;
    [btn2 setTitleColor:SDColor(69, 69, 69) forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"person_phone"] forState:UIControlStateNormal];
    [[btn2 rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self clickPhoneLogin];
    }];
    self.phoneBtn=btn2;
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.top.equalTo(self.view.mas_bottom).offset(20);
        make.height.mas_equalTo(50);
    }];
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-80);
        make.height.mas_equalTo(50);
    }];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.bottom.equalTo(btn2.mas_top).offset(-20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    [imgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.top.equalTo(self.view.mas_top).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.bottom.equalTo(btn2.mas_top).offset(-20);
    }];
    
    [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.equalTo(imgv.mas_bottom).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.height.mas_equalTo(0);
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - EventDeal

-(void)clickPhoneLogin
{
    [UIView animateWithDuration:1.0 animations:^{
        self.inputView.alpha=1.0;
        self.inputView.status=1;
        self.weixinBtn.alpha=0.0;
        self.phoneBtn.alpha=0.0;
        self.logoImgV.transform=CGAffineTransformMakeScale(0.5, 0.5);
        
    } completion:^(BOOL finished) {
 
    }];
    
}

#pragma mark - LazyLoad

-(UserData *)user
{
    if(_user==nil)
    {
        _user=[[UserData alloc]init];
    }
    return _user;
}
@end
